package com.fadadianqi.logistics.admin.staff.service;

import com.fadadianqi.logistics.admin.staff.dao.MasterDAO;
import com.fadadianqi.logistics.admin.staff.model.Action;
import com.fadadianqi.logistics.admin.staff.model.Group;
import com.fadadianqi.logistics.admin.staff.model.Master;
import com.fadadianqi.commons.ConfigHelper;
import com.fadadianqi.commons.PrimaryKeyCreator;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MasterService {

    /**
     *
     */
    public void init() {
        String name = ConfigHelper.getString("master.name");
        String password = ConfigHelper.getString("master.password");
        if (ValidatorHelper.isNotEmpty(name)
                && ValidatorHelper.isNotEmpty(password)) {
            Master m = getByName(name);
            if (m == null) {
                m = new Master();
                m.setName(name);
                m.setPassword(password);
                try {
                    save(m);
                    masterGroupService.masterToAllGroup(m);
                    ConfigHelper.del("master.name");
                    ConfigHelper.del("master.password");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Master getByName(String name) {
        return masterDAO.getByName(name);
    }

    public Master getById(String id) {
        return masterDAO.getById(id);
    }

    public void save(Master master) throws Exception {
        if (ValidatorHelper.isEmpty(master.getId())) {
            if (getByName(master.getName()) != null) {
                throw new Exception("exception.duplicate.master.name");
            }
            master.setId(PrimaryKeyCreator.create());
            masterDAO.create(master);
        } else {
            Master t = getByName(master.getName());
            if (t != null && !t.getId().equalsIgnoreCase(master.getId())) {
                throw new Exception("exception.duplicate.master.name");
            }
            masterDAO.update(master);
        }
    }

    public List<Master> findAll() {
        return masterDAO.findAll();
    }

    public Map<String, Action> findActions(Master m) {
        Map<String, Action> map = new HashMap<String, Action>();
        List<Group> groups = masterGroupService.findGroupsByMaster(m);
        if (ValidatorHelper.isNotEmpty(groups)) {
            for (Group g : groups) {
                List<Action> actions = groupActionService.findByGroup(g);
                if (ValidatorHelper.isEmpty(actions)) {
                    continue;
                }
                for (Action a : actions) {
                    map.put(a.getId(), a);
                }
            }
        }
        return map;
    }

    public boolean hasPermission(Master m, ActionEnum e) {
        String mode = ConfigHelper.getString("master.mode");
        if ("demo".equals(mode)) {
            // 系统调试模式所有权限返回 TRUE
            return Boolean.TRUE;
        }
        if (m == null || e == null) {
            return Boolean.FALSE;
        }
        return this.findActions(m).get(e.toString()) != null;
    }

    @Autowired
    private GroupActionService groupActionService;

    @Autowired
    private MasterGroupService masterGroupService;

    @Autowired
    private MasterDAO masterDAO;
}
